import * as order from "../../api/order";

Page({
    data: {
        tab_list:[{
            label:"全部",
            value:""
        },{
            label:"待支付",
            value:"1"
        },{
            label:"待发货",
            value:"2"
        },{
            label:"已发货",
            value:"3"
        },{
            label:"已完成",
            value:"4"
        },{
            label:"已关闭",
            value:"5"
        }],
        
        tabIndex:0,
        currentPage:1,
        createTime:[],
        keyword:"",
        list:[],
        currentPage:1,
        hasNextPage:true,
        total:"",
        orderStatus:""
    },
    onLoad(){

        // this.setData({
        //     hasNextPage:true,
        //     total:"",
        //     currentPage:1,
        //     createTime:[],
        //     tabIndex:0
        // })

        this.get_order_list();
    },
    get_order_list(){

        let createTime =  this.data.createTime.length == 0 ? '' : this.data.createTime.join(",");

        order.orderList({
            currentPage:this.data.currentPage,
            pageSize:10,
            createTime:createTime,
            keyword:this.data.keyword,
            orderStatus:this.data.orderStatus
        })
        .then((res)=>{

            this.setData({
                list:this.data.currentPage == 1 ? res.list :[...this.data.list, ...res.list],
                hasNextPage:res.hasNextPage,
                total:res.total
            })
        })
    },
    top_picker_confirm(e){

        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            createTime:e.detail
        })

        this.get_order_list();
    },
    handle_tab_change(e){
        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            orderStatus:e.detail
        })

        this.get_order_list();
    },
    handle_top_search(e){
        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            keyword:e.detail.value
        })

        this.get_order_list();
    },
    handle_order_detail(e){
        let { item } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/order-detail/order-detail?orderNo=' + item.orderNo
        });
    },
    handle_copy_text(e){
        let { orderNo } = e.currentTarget.dataset.item;
        wx.setClipboardData({
            data: orderNo
        });
    },
    onReachBottom(){
        if(this.data.hasNextPage){
            this.setData({
                currentPage: ++this.data.currentPage
            })
            this.get_order_list();
        }
    }
})
